
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

-- EditBox
function MainScene:testEditBox()
    ccui.EditBox:create(cc.size(200, 40), "editbox.png")
        :addTo(self)
        :move(display.cx, display.cy)
        :setPlaceHolder("文本输入控件")
        :setPlaceholderFontColor(cc.c3b(0, 0, 0))
        :registerScriptEditBoxHandler(function(event, source)
                if event == "began" then
                    print("开始输入")
                elseif event == "ended" then
                    print("输入结束")
                elseif event == "changed" then
                    print("输入框内容发生变化")
                elseif event == "return" then
                    print("从输入框返回，内容是", source:getText())
                end
            end)

    ccui.EditBox:create(cc.size(200, 40), "editbox.png")
        :addTo(self)
        :move(display.cx, display.cy - 100)
        :setPlaceHolder("请输入密码")
        :setPlaceholderFontColor(cc.c3b(0, 0, 0))
        :setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)
        :registerScriptEditBoxHandler(function(event, source)
                if event == "began" then
                    print("开始输入")
                elseif event == "ended" then
                    print("输入结束")
                elseif event == "changed" then
                    print("输入框内容发生变化")
                elseif event == "return" then
                    print("从输入框返回，内容是", source:getText())
                end
            end)
end

-- LoadingBar
function MainScene:testLoadingBar()
    local bg = display.newSprite("loading-bg.png")
        :addTo(self)
        :move(display.cx, display.cy)
    
    local bar = ccui.LoadingBar:create("loading.png")
        :addTo(self)
        :move(display.cx, display.cy)
        :setScale9Enabled(true)

    local size = bar:getVirtualRendererSize()
    bg:setContentSize(size)

    local progress = 0
    bar:setPercent(progress)
    local scheduler = self:getScheduler()
    scheduler:scheduleScriptFunc(function()
        progress = progress + 1
        if progress > 100 then progress = 0 end
        bar:setPercent(progress)
    end, 0.05, false)
end

function MainScene:testSlider()
    ccui.Slider:create("slider/SliderBar.png", "slider/SliderButton.png")
        :addTo(self)
        :move(display.cx - display.width/4, display.cy)
        :setScale9Enabled(true)
        :setContentSize(200, 40)
        :addEventListener(function(source, event)
                if event == 2 then -- ON_SLIDEBALL_UP
                    print("the horizontal slider value:", source:getPercent())
                end
            end)

    ccui.Slider:create("slider/SliderBar.png", "slider/SliderButton.png")
        :addTo(self)
        :move(display.cx + display.width/4, display.cy)
        :setScale9Enabled(true)
        :setContentSize(200, 40)
        :setRotation(90)
        :addEventListener(function(source, event)
                if event == 2 then -- ON_SLIDEBALL_UP
                    print("the vertical slider value:", source:getPercent())
                end
            end)
end

function MainScene:testScrollView()
    local sp = display.newSprite("scroll/cloud.png")
    local size = sp:getContentSize()
    sp:move(size.width/2, size.height/2)

    local viewsize = cc.size(display.width/2, display.height/2)
    ccui.ScrollView:create()
        :addTo(self)
        :move(display.cx - viewsize.width/2, display.cy - viewsize.height/2)
        :setContentSize(viewsize)
        :setDirection(ccui.ScrollViewDir.both)
        :setBounceEnabled(false)
        :setInnerContainerSize(size)
        :addChild(sp)
        :addEventListener(function(source, event)
                print("scroll event:", event) -- from SCROLL_TO_TOP to AUTOSCROLL_ENDED
            end)
end

function MainScene:testListView()
    local viewsize = cc.size(display.width/2 - 50, display.height/2)

    local function addItem(lv, num)
        local content, layout, size
        for i = 1, num do
            layout = ccui.Layout:create()
            if i == 1 then
                content = cc.Label:createWithSystemFont("item" .. i, "", 20)
                
            elseif i == 2 then
                content = ccui.Button:create("list/GreenButton.png")
                local texsize = content:getNormalTextureSize()
                content:setScale9Enabled(true)
                    :setTitleText("点击大小改变" .. i)
                    :setTitleFontSize(16)
                    :setContentSize(texsize.width*3, texsize.height)
                    :addClickEventListener(function(source) -- 按钮的clicked事件处理
                        if source:getContentSize().width == texsize.width*3 then
                            source:setContentSize(texsize.width*2, texsize.height)
                        else
                            source:setContentSize(texsize.width*3, texsize.height)
                        end
                    end)

            elseif i == 3 then
                content = cc.Label:createWithSystemFont("文本信息" .. i, "", 20)

            elseif i == 4 then
                content = display.newSprite("list/sunset.png")

            else
                content = cc.Label:createWithSystemFont("item" .. i, "", 20)
            end

            size = content:getContentSize()
            content:move(size.width/2, size.height/2)
            layout:setContentSize(size)
            layout:addChild(content)
            lv:pushBackCustomItem(layout)
        end
    end

    local vertical = ccui.ListView:create()
        :addTo(self)
        :move(display.cx - display.width/4 - viewsize.width/2, display.cy - viewsize.height/2)
        :setContentSize(viewsize)
        :setDirection(ccui.ScrollViewDir.vertical)
        :setBounceEnabled(false)
        :setBackGroundColorType(ccui.LayoutBackGroundColorType.solid)
        :setBackGroundColor(cc.c3b(128, 0, 0))
        :setItemsMargin(5)
    addItem(vertical, 50)

    
    local horizontal = ccui.ListView:create()
        :addTo(self)
        :move(display.cx + display.width/4 - viewsize.width/2, display.cy - viewsize.height/2)
        :setContentSize(viewsize)
        :setDirection(ccui.ScrollViewDir.horizontal)
        :setBounceEnabled(false)
        :setBackGroundImageScale9Enabled(true)
        :setBackGroundImage("list/YellowBlock.png")
        :setItemsMargin(5)
    addItem(horizontal, 10)
end

function MainScene:testPageView()
    math.newrandomseed()

    local viewsize = cc.size(480, 480)
    local view = ccui.PageView:create()
        :addTo(self)
        :move(display.cx - viewsize.width/2, display.cy - viewsize.height/2)
        :setContentSize(viewsize)
        :setDirection(ccui.ScrollViewDir.horizontal)
        :setIndicatorEnabled(true)
    
    local layerWidth = 140
    local row, col = 3, 3
    local layout
    for page = 1, 3 do
        layout = ccui.Layout:create()
        view:addPage(layout)

        for i = 1, row do
            for j = 1, col do
                local content = cc.LayerColor:create(cc.c4b(math.random(255), math.random(255), math.random(255), 255))
                content:setContentSize(layerWidth, layerWidth)
                    :move((j-1)*layerWidth, (i-1)*layerWidth)

                cc.Label:createWithSystemFont("item" .. ((i-1)*col+j), "", 20)
                    :align(display.CENTER, content:getContentSize().width/2, content:getContentSize().height/2)
                    :addTo(content)

                layout:addChild(content)
            end
        end
    end
end

function MainScene:onCreate()
    -- 注释函数，查看不同的运行效果
    -- self:testEditBox()
    -- self:testLoadingBar()
    -- self:testSlider()
    -- self:testScrollView()
    -- self:testListView()
    self:testPageView()
end

return MainScene
